package com.oro.backingbeans;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.oro.domain.Cliente;
import com.oro.service.CustomerService;

public class InserisciClienteBean {

	private Cliente clienteInput;

	private Cliente clienteOutput;
	@Autowired
	private CustomerService customerService;

	private Logger logger;

	public InserisciClienteBean() {
		super();
		clienteInput = new Cliente();
		clienteOutput = new Cliente();
	}

	public Cliente getClienteInput() {
		return clienteInput;
	}

	public Cliente getClienteOutput() {
		return clienteOutput;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	private Logger getLogger() {
		if (logger == null) {
			logger = Logger.getLogger(InserisciClienteBean.class);
		}
		return logger;
	}

	public String inserisciCliente(Cliente cliente) {
		customerService.save(cliente);
		return "inserisciClienteSuccess";
	}

	@PostConstruct
	public void postConstruct() {
		getLogger().info("ClientiBean has been constructed.");
	}

	@PreDestroy
	public void preDestroy() {
		getLogger().info("ClientiBean is being destroyed.");
	}

	public void setClienteInput(Cliente clienteInput) {
		this.clienteInput = clienteInput;
	}

	public void setClienteOutput(Cliente clienteOutput) {
		this.clienteOutput = clienteOutput;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public String submit() {
		customerService.save(clienteInput);
		clienteOutput = clienteInput;
		clienteInput = new Cliente();
		return "inserisciClienteSuccess?faces-redirect=true";
	}

}
